package com.trailwalker.traceability.widget.dialog;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import com.trailwalker.javafx.widget.dialog.CustomDialog;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.ArrayList;

/**
 * @author Trailwalker
 * @create 2021-11-11 17:53
 */
public class ConfirmDialog {

    private ConfirmDialogConfig mConfirmDialogConfig;
    private AdaptUtils mAdaptUtils;
    private AnchorPane mApContent;
    private CustomDialog mCustomDialog;
    private Button mBtnPositive;

    private ConfirmDialog(ConfirmDialogConfig confirmDialogConfig) {
        mConfirmDialogConfig = confirmDialogConfig;
        onInit();
    }

    private void onInit() {
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    public void show() {
        mCustomDialog.show();
    }

    public void dismiss() {
        mCustomDialog.dismiss();
    }

    private void onReady() {
        CustomDialog.CustomDialogBuilder customDialogBuilder = new CustomDialog.CustomDialogBuilder(mConfirmDialogConfig.primaryStage);
        customDialogBuilder.setPrimaryColor("#026cba");
        String styleSheet = ResourceUtils.getCss(this, "lightTheme.css");
        ArrayList<String> styleSheets = new ArrayList<>();
        styleSheets.add(styleSheet);
        customDialogBuilder.setStylesheets(styleSheets);
        if (mConfirmDialogConfig.dialogWidth != null) {
            customDialogBuilder.setDialogWidth(mConfirmDialogConfig.dialogWidth);
        }
        if (mConfirmDialogConfig.dialogHeight != null) {
            customDialogBuilder.setDialogHeight(mConfirmDialogConfig.dialogHeight);
        }
        customDialogBuilder.setContentView(mApContent);
        if (StringUtils.isValid(mConfirmDialogConfig.titleText)) {
            customDialogBuilder.setTitleText(mConfirmDialogConfig.titleText);
        }
        if (mConfirmDialogConfig.titleTextPos != null) {
            customDialogBuilder.setTitleTextPos(mConfirmDialogConfig.titleTextPos);
        } else {
            customDialogBuilder.setTitleTextPos(Pos.CENTER);
        }
        mCustomDialog = customDialogBuilder.build();
    }

    private void initListener() {
        if (mBtnPositive != null) {
            mBtnPositive.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        if (mConfirmDialogConfig.onButtonClickListener != null) {
                            mConfirmDialogConfig.onButtonClickListener.onClick(ConfirmDialog.this);
                        }
                    }
                }
            });

            mBtnPositive.setOnKeyReleased(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent event) {
                    if (event.getCode() == KeyCode.ENTER) {
                        if (mConfirmDialogConfig.onButtonClickListener != null) {
                            mConfirmDialogConfig.onButtonClickListener.onClick(ConfirmDialog.this);
                        }
                    }
                }
            });
        }
    }

    private void initData() {

    }

    private void initView() {
        mApContent = new AnchorPane();
        StyleUtils.setBackgroundColor(mApContent, "#eeeeee");

        BorderPane bpContent = new BorderPane();
        mApContent.getChildren().add(bpContent);
        mAdaptUtils.setMatchParentAnchorPane(bpContent);

        HBox hbCenter = new HBox();
        bpContent.setCenter(hbCenter);
        hbCenter.setAlignment(Pos.CENTER);
        mAdaptUtils.setScalePaddingTopAndBottom(hbCenter, 25);

        if (StringUtils.isValid(mConfirmDialogConfig.contentText)) {
            Label lblContent = new Label();
            hbCenter.getChildren().add(lblContent);
            lblContent.setText(mConfirmDialogConfig.contentText);
            String contentTextColor;
            if (StringUtils.isValid(mConfirmDialogConfig.contentTextColor)) {
                contentTextColor = mConfirmDialogConfig.contentTextColor;
            } else {
                contentTextColor = "#000000";
            }
            StyleUtils.setTextColor(lblContent, contentTextColor);
            int scaleTextSize;
            if (mConfirmDialogConfig.scaleTextSize != null) {
                scaleTextSize = mConfirmDialogConfig.scaleTextSize;
            } else {
                scaleTextSize = 30;
            }
            mAdaptUtils.setScaleTextSize(lblContent, scaleTextSize);
        }

        HBox hbBottom = new HBox();
        bpContent.setBottom(hbBottom);
        hbBottom.setAlignment(Pos.CENTER);
        mAdaptUtils.setScalePaddingBottom(hbBottom, 25);

        mBtnPositive = new Button();
        hbBottom.getChildren().add(mBtnPositive);
        mBtnPositive.getStyleClass().add("positive_button");
        mAdaptUtils.setScaleSize(mBtnPositive, 130, 40);
        StyleUtils.setBorderRadius(mBtnPositive, mAdaptUtils.getScale(130), mAdaptUtils.getScale(40), mAdaptUtils.getScale(10));
        StyleUtils.setTextColor(mBtnPositive, "#ffffff");
        mAdaptUtils.setScaleTextSize(mBtnPositive, 20);
        mBtnPositive.setCursor(Cursor.HAND);
        if (StringUtils.isValid(mConfirmDialogConfig.buttonText)) {
            mBtnPositive.setText(mConfirmDialogConfig.buttonText);
        }
    }

    private void initLoad() {
        mAdaptUtils = AdaptUtils.getInstance();
    }

    public static class ConfirmDialogBuilder {
        private ConfirmDialogConfig mConfirmDialogConfig;

        public ConfirmDialogBuilder(Stage primaryStage) {
            mConfirmDialogConfig = new ConfirmDialogConfig();
            mConfirmDialogConfig.primaryStage = primaryStage;
        }

        public ConfirmDialogBuilder setDialogWidth(double dialogWidth) {
            mConfirmDialogConfig.dialogWidth = dialogWidth;
            return this;
        }

        public ConfirmDialogBuilder setDialogHeight(double dialogHeight) {
            mConfirmDialogConfig.dialogHeight = dialogHeight;
            return this;
        }

        public ConfirmDialogBuilder setTitleText(String titleText) {
            mConfirmDialogConfig.titleText = titleText;
            return this;
        }

        public ConfirmDialogBuilder setTitleTextPos(Pos titleTextPos) {
            mConfirmDialogConfig.titleTextPos = titleTextPos;
            return this;
        }

        public ConfirmDialogBuilder setContentText(String contentText) {
            mConfirmDialogConfig.contentText = contentText;
            return this;
        }

        public ConfirmDialogBuilder setScaleTextSize(int scaleTextSize) {
            mConfirmDialogConfig.scaleTextSize = scaleTextSize;
            return this;
        }

        public ConfirmDialogBuilder setContentTextColor(String contentTextColor) {
            mConfirmDialogConfig.contentTextColor = contentTextColor;
            return this;
        }

        public ConfirmDialogBuilder setButtonText(String buttonText) {
            mConfirmDialogConfig.buttonText = buttonText;
            return this;
        }

        public ConfirmDialogBuilder setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
            mConfirmDialogConfig.onButtonClickListener = onButtonClickListener;
            return this;
        }

        public ConfirmDialog build() {
            return new ConfirmDialog(mConfirmDialogConfig);
        }

    }

    private static class ConfirmDialogConfig {
        Stage primaryStage;
        Double dialogWidth;
        Double dialogHeight;
        String titleText;
        Pos titleTextPos;
        String contentText;
        Integer scaleTextSize;
        String contentTextColor;
        String buttonText;
        OnButtonClickListener onButtonClickListener;
    }

    public interface OnButtonClickListener {
        void onClick(ConfirmDialog confirmDialog);
    }

}
